home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: in1.uu.net!world!bgw
- From: bgw@world.std.com (Bruce G Wilson)
- Subject: g++/templates: duplicate symbols
- Message-ID: <Dq275z.A2t@world.std.com>
- Organization: The World Public Access UNIX, Brookline, MA
- Date: Thu, 18 Apr 1996 13:08:22 GMT
-
-
- I have a problem compiling some C++ template code under g++. I'm getting
- duplicate definitions of symbols in the link stage. The problem appears
- involve a class that uses two template instantiations in it. I'm
- instantiating each template class in a separate file, but because I'm
- including the .h file for the containing class in each file, I'm getting
- *both* template instantiations in *each* file, which creates duplicate
- symbol definitions at link time.
-
- Here's a sketch of what the code is like:
-
- // foo.h
- class A {
- // ...
- private:
- LinkedList<String> _list;
- };
-
- class B {
- // ...
- private:
- Dictionary<String, A> _dict;
- };
-
-
- // foo.cpp
- (implementations of methods from classes A and B)
-
-
- // LinkedListString.cpp
- #include "String.h"
- #include "LinkedList.h"
- #include "LinkedList.cpp"
-
- #include "foo.h"
-
- template class LinkedList<String>;
-
-
- // DictionaryStringA.cpp
- #include "String.h"
- #include "Dictionary.h"
- #include "Dictionary.cpp"
-
- #include "foo.h"
-
- template class Dictionary<String, A>;
-
-
- foo.cpp is compiled with "-fno-implicit-templates".
-
- The files that instantiate the template classes (LinkedListString.cpp
- and DictionaryStringA.cpp) are compiled without the "-fno-implicit-templates"
- option. When I link, I get duplicate symbol definitions for some of
- the template class methods.
-
- The duplicate symbols appear to be arising because "foo.h" is included
- in the two files that instantiate templates. The classes in foo.h define
- two template instantiations, and both are instantiated in each of the
- files. So, in other words, I get one instantiation of the code for
- "LinkedList<String>" and "Dictionary<String, A>" in each of the files
- LinkedListString.o and DictionaryStringA.o .
-
- The workaround is to put all template instantiations for foo.h in one
- template instantiation file:
-
- AllTemplates.cpp:
-
- #include "String.h"
- #include "LinkedList.h"
- #include "LinkedList.cpp"
- #include "Dictionary.h"
- #include "Dictionary.cpp"
-
- #include "foo.h"
-
- template class LinkedList<String>;
- template class Dictionary<String, A>;
-
-
- This works, but it's not a neat solution because LinkedList<String> is
- a composition of two generic types which might eventually be instantiated
- elsewhere in the code; I'd like to be able to place template instantiations
- like these in a common area where they're built once and can be used in
- many places. Otherwise, the next time someone uses a LinkedList<String>
- in their code, there will be duplicate symbol definitions all over again.
-
- I seem to recall that other C++ compilers resolve this problem by using
- a special "pre-link" phase that resolves redundant template instantiations
- like this. Does g++ not do this ? Does it just call the standard 'ld'
- linker ? Is there an alternate linker that comes with g++ that I could call
- which would handle template instantiation correctly ?
-
- I've seen reference in some of the online documentation for g++ about a
- "template repository" feature that will be part of a future release of
- g++. Will this feature handle the kind of problems I'm having ?
-
- I'm working with g++ 2.6.3 on BSDI (ix86 Unix).
-